import java.io.*;
import java.net.*;
import java.util.*;

import com.google.common.net.*;
	
public class Url2Re {
	private String re;
	private String domain;
	private String date;
	/*** Re parts ***/
	private static final String[] dateParts={
		"",
		"2012",
		"0[7-9]",
		"[0-9]{2}",

	};
	private static final String[] littleParts={
		"",
		"/",
		"\\-",
		"*",
		"?",
		"+",
		"(",
		")"
	};
	private static final String[] commonParts = {
		    "",
			"^http://",
			"([0-9a-zA-Z]+\\.)*",
			"([^/]+/)*",
			"[-0-9a-zA-Z_]+",
			"\\.s?html?",
			"[-0-9_]"
	};
	
	/*** translates ***/
	private int transDate(char abbr){
		switch(abbr){
		case 'Y': return 1;
		case 'M': return 2;
		case 'D': return 3;

		default : return 0;
		}
	}
	private int transLittle(char abbr){
		switch(abbr){
		case '/': return 1;
		case '-': return 2;
		case '*': return 3;
		case '?': return 4;
		case '+': return 5;
		case '(': return 6;
		case ')': return 7;
		default : return 0;
		}
	}
	private int transCommon(char abbr){
		switch(abbr){
		case 'h': return 1;
		case 'p': return 2;
		case 'm': return 3;
		case 's': return 4;
		case 't': return 5;
		case 'd': return 6;
		default : return 0;
		}
	}
	
	private String trans(char abbr){
		if(abbr<='Z' && abbr>='A')
			return dateParts[transDate(abbr)];
		if(abbr<='z' && abbr>='a')
			return commonParts[transCommon(abbr)];
		if(abbr == '.')
			return this.domain;
		return littleParts[transLittle(abbr)];
		
	}
	
	
	public Url2Re(String domain,char[] args,int size){
		this.domain = domain.replaceAll("\\.", "\\\\.").concat("/");
		this.re = "";
		int start=0,end=0;
		boolean flag=false;
		for(int i = 0;i<size;i++){
			String part = "";
			/*** recognize meaning of label '\' ***/
			if(args[i]=='\''){
				while(true)
				{
					++i;
					if(args[i]=='\\')
					{
						this.re+=args[++i];	
						continue;
					}
					if(args[i]=='\'')
					{
						break;
					}
					
					this.re+=args[i];
				}
				continue;
			}
			part = trans(args[i]);
			this.re += part;
			
			/***  get date's pos in re  ***/
			if(args[i]=='Y'||args[i]=='M'||args[i]=='D'){
				if(flag==false){
					flag=true;
					start=this.re.length()-part.length();
					
				}
				if(flag==true){
					end = this.re.length();
				}
				
			}
		}
		/***  get date form re  ***/
		//System.out.println(start+"   "+end);
		if(start!=end)
			this.date=this.re.substring(start, end)
									.replace(trans('Y'), "("+trans('Y')+")")
									.replace(trans('M'), "("+trans('M')+")")
									.replace(trans('D'), "("+trans('D')+")");
		else this.date = "";
	};
	public String getRe(){
		return this.re;
	}
	
	public static void Urls2Res(String infile,String outfile) throws IOException{
		BufferedReader in = new BufferedReader(new FileReader(infile));
		String line;
		while((line = in.readLine())!=null)
		{
			generateRe(line.split("\\s+"),outfile);
		}
		
	}
	
	public static void generateRe(String args[],String outfile) throws IOException{
		final int len = args.length;
		if(len!=4&&len!=3) 
		{
			System.out.format("Wrong args length invalid!",len);
			return;
		}
		int count = 0;
		int n = 0;
		if(len==4){
			n = Integer.parseInt(args[count++]);
		}
		
		Url2Lds u2l = new Url2Lds(args[count++]);
		int level = Integer.parseInt(args[count++]);
		char[] arr = args[count].toCharArray();
		String domain = u2l.get(level); 
		File f = new File(outfile);
		Url2Re u2r = new Url2Re(domain, arr, arr.length);
		BufferedWriter out = new BufferedWriter(new FileWriter(f,true));
		if(n!=0)
			out.append(n+"\t"+u2r.getRe()+"\t"+u2r.date+"\n");
		else
			out.append(u2r.getRe()+"\t"+u2r.date+"\n");
		out.flush();
		out.close();
		//System.out.println(n+"\t"+u2r.getRe());
	}
	public static void main(String args[]) throws IOException
	{
		System.out.println(args[0]+"  "+args[1]);
		Url2Re.Urls2Res(args[0], args[1]);
	}

}
	
	
